package com.kkb.controller;
import com.kkb.pojo.Admins;
import com.kkb.service.AdminsService;
import com.kkb.service.LoginService;
import com.kkb.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/admin")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private AdminsService adminsService;
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public ResultVo<Admins> login(Admins admins,  HttpSession session){
        if ((admins.getLoginName()!=null && !"".equals(admins.getLoginName().trim()))&&
            (admins.getAdminPwd()!=null && !"".equals(admins.getAdminPwd().trim()))){
            Admins admins1 = loginService.login(admins);
            if (admins1!=null){
                //session.setAttribute("loginName",admins.getLoginName());
                session.setAttribute("adminId",admins1.getAdminId());
                return new ResultVo<>();
            }else{
                return new ResultVo<>(500,"请输入正确的账号或者密码");
            }
        }else{
            return new ResultVo<>(500,"请输入登录账号或密码");
        }
    }
    /**
     * 通过session里面存的信息去查找信息
     */
    @RequestMapping(value = "/infos",method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<Admins> queryById(HttpSession httpSession){
        Integer adminId = (Integer) httpSession.getAttribute("adminId");
        Admins admins = adminsService.queryById(adminId);
        return new ResultVo<>(admins);
    }
    /**
     * 退出登录，清空session,并返回登录界面
     */
    @RequestMapping(value = "/out",method = RequestMethod.GET)
    @ResponseBody
    public void  logout(HttpSession httpSession, HttpServletResponse response) {
        httpSession.invalidate();
        //重定向到登录界面,为什么去掉呢，因为拦截器中已经有相应的跳转方法了
        //return "redirect:/pages/login.html";
    }


}
